<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use DOMPDFModule\View\Model\PdfModel;

class UsuarioController extends AbstractActionController
{

    protected $loginTable;

    public function listarAction()
    {
        $sm        = $this->getServiceLocator();
        $db        = $sm->get( 'db' );
        $page      = $this->getRequest()->getPost( 'page', 1 );
        $business  = new \Admin\Model\Business\Usuario( $db );
        $paginator = $business->grid( $page );
        $result    = new ViewModel();
        $result->setTerminal( true );
        $result->setVariables( array('paginator' => $paginator) );
        return $result;

    }

    public function novoAction()
    {
        $form = new \Admin\Form\Usuario();
        return new ViewModel( array('form' => $form) );

    }

    public function salvarAction()
    {
        $form    = new \Admin\Form\Usuario();
        $request = $this->getRequest();
        if ($request->isPost()) {
            $usuario = new \Admin\Filter\Usuario();
            $form->setInputFilter( $usuario->getInputFilter() );

            $form->setData( $request->getPost() );
            $form->remove( 'gravar' );

            if ($form->isValid()) {
                $data = $form->getData();
            }
        }
        $sm = $this->getServiceLocator();
        $db = $sm->get( 'db' );

        $business = new \Admin\Model\Business\Usuario( $db );
        if (empty( $data['id'] )) {
            $business->insert( $data );
        } else {
            $business->atualizar( $data );
        }


        $json = new JsonModel( array(
            'type'    => 'success', 'message' => 'Inserido com sucesso') );
        return $json;

    }

    public function editarAction()
    {
        $id = $this->params()->fromQuery( 'id' );
        $sm = $this->getServiceLocator();
        $db = $sm->get( 'db' );

        $business = new \Admin\Model\Business\Usuario( $db );
        $data     = $business->listarDadosUsuario( $id );
        $form     = new \Admin\Form\Usuario();
        $form->populateValues( $data[0] );
        return new ViewModel( array('form' => $form) );

    }

    public function deletarAction()
    {
        $id = $this->params()->fromPost( 'id' );
        $sm = $this->getServiceLocator();
        $db = $sm->get( 'db' );

        $business = new \Admin\Model\Business\Usuario( $db );
        $business->excluir( $id );
        $json     = new JsonModel( array(
            'type'    => 'success', 'message' => 'Excluído com sucesso') );
        return $json;

    }

    public function reportAction()
    {
        $pdf      = new PdfModel();
        $sm        = $this->getServiceLocator();
        $db       = $sm->get( 'db' );
        $business = new \Admin\Model\Business\Usuario( $db );
        $data = $business->report();
        
        $pdf->setVariables( array(
            'data' => $data,
        ) );
        return $pdf;

    }

}
